#include "DeltaGlider.h"
#include "OrbiterSoundSDK35.h"
// --------------------------------------------------------------
// Frame update
// --------------------------------------------------------------
void DeltaGlider::clbkPostStep (double simt, double simdt, double mjd)
{
	
	
 

	
	
	

	/*
	 
	jet(getEngineMode());
	if (  ((GetAltitude()) > 18288) && (getEngineMode())  ) //18288 meters = 60,000 feet this is where I think this engine can go
		revertEngine(3);                                    // 15240 meters = 50,000
	if (  ((GetAltitude()) > 30240) && (getEngineMode())  )
		revertEngine(2);
	if (getEngineMode()){
	handleJet(0);
	handleJet(1);}
	*/


     
    
	battery_draw_001 = 0;
	battery_draw_002 = 0;
	battery_draw_003 = 0;
	battery_draw_004 = 0;
	
	LifeSupportSystem();
	
	
	if (BatteryIsCharging()) {

	charging_status   = DOOR_OPEN;	
	}                              

	else{
	charging_status   = DOOR_CLOSED;	
	}
    
	
//double time_hit = simdt;

    //if  id oapiSetHUDMode(0);

	//double x = simdt;
	
	ReturnCode = Crew.ProcessUniversalMMu();
	
	VECTOR3 v;
	double hspd, vspd;
	if (GetHorizonAirspeedVector (v)) {
		hspd = _hypot (v.x, v.z);
		vspd = v.y;
	}
	alt = GetAltitude();

	// calculate max scramjet thrust
	if (getEngineMode() == 4) ScramjetThrust();

	
	
	if ((HUD_PANEL_ID == 0)&& (CHECK_HUD_PANEL_STATE == 1))
	{
		oapiSetHUDMode(HUD_PANEL_STATE);
		CHECK_HUD_PANEL_STATE = 2;
	}

	//PARKING_BRAKE
    if ((PARKING_BRAKE == SYSTEM_ON) && (POWER == SYSTEM_ON))
	{
           SetWheelbrakeLevel (93.0, 0, true);
	}

    if ((PARKING_BRAKE == SYSTEM_OFF) && (POWER == SYSTEM_ON))
	{
           SetWheelbrakeLevel (0, 0, true);
	}

//BEACON_LIGHTS_POWER
	if ((BEACON_LIGHT_POWER == SYSTEM_ON) && (POWER == SYSTEM_ON))
	{
           SetBeacon(true);
	}

    if ((BEACON_LIGHT_POWER == SYSTEM_OFF) && (POWER == SYSTEM_ON))
	{
            SetBeacon(false);
	}	

	
//NAVIGATION_LIGHTS_POWER
	if ((NAVIGATION_LIGHTS_POWER == SYSTEM_ON) && (POWER == SYSTEM_ON))
	{
           SetNavlight (true);
	}

    if ((NAVIGATION_LIGHTS_POWER == SYSTEM_OFF) && (POWER == SYSTEM_ON))
	{
            SetNavlight (false);
	}


//STROBE_LIGHTS_POWER
	if ((STROBE_LIGHTS_POWER == SYSTEM_ON) && (POWER == SYSTEM_ON))
	{
           SetStrobe (true);
	}

    if ((STROBE_LIGHTS_POWER == SYSTEM_OFF) && (POWER == SYSTEM_ON))
	{
            SetStrobe (false);

	}    

	

//  TURBINE_RUNNING_LEFT start------------------------------------------------------------------  

	if ((TURBINE_RUNNING_LEFT == SYSTEM_OFF) && (TURBINE_START_LEFT_POWER == SYSTEM_ON))
	{
		
		PlayVesselWave3(MyID,jet_starting,NOLOOP,225,0);
    
				               //timer for start on jet
                               leftstartjet = oapiGetSimTime();
                               leftstartjet = leftstartjet + 7.75;
				 

                               		 


      TURBINE_STARTING_LEFT = SYSTEM_ON;
      TURBINE_RUNNING_LEFT = SYSTEM_ON;


	}
    
	TURBINE_STARTING_LEFT_TIMER = oapiGetSimTime();
	

	

	if ((TURBINE_STARTING_LEFT == SYSTEM_ON)&& (TURBINE_STARTING_LEFT_TIMER >= leftstartjet))      // jet sound starting has been played for 7.75 sec and is .25 
	{		                                                                                      // and is .25 seconds from being finished

		        
             // This is where we add the code to turn on the jet 


              TURBINE_STARTING_LEFT = SYSTEM_OFF;
	}
	
//  TURBINE_RUNNING_LEFT start------------------------------------------------------------------ 


//  TURBINE_RUNNING_right start------------------------------------------------------------------  
	if ((TURBINE_RUNNING_RIGHT == SYSTEM_OFF) && (TURBINE_START_RIGHT_POWER == SYSTEM_ON))
	{
		
		PlayVesselWave3(MyID,jet_starting,NOLOOP,225,0);
    
				               //timer for start on jet
                               rightstartjet = oapiGetSimTime();
                               rightstartjet = rightstartjet + 7.75;
				 
      TURBINE_STARTING_RIGHT = SYSTEM_ON;
      TURBINE_RUNNING_RIGHT = SYSTEM_ON;


	}
    
	TURBINE_STARTING_RIGHT_TIMER = oapiGetSimTime();
	
	if ((TURBINE_STARTING_RIGHT == SYSTEM_ON)&& (TURBINE_STARTING_RIGHT_TIMER >= rightstartjet))  // jet sound starting has been played for 7.75 sec and is .25 
	{		                                                                                      // and is .25 seconds from being finished

		      // This is where we add the code to turn on the jet
             
		TURBINE_STARTING_RIGHT = SYSTEM_OFF;
	}
	
//  TURBINE_RUNNING_right start------------------------------------------------------------------ 






	// engine gimbal adjustments
	if (mpmode) AdjustMainPGimbal (mpmode);
	if (mymode) AdjustMainYGimbal (mymode);
	if (spmode) AdjustScramGimbal (spmode);
	if (hbmode) AdjustHoverBalance (hbmode);
	
		// animate landing gear
		if (gear_status >= DOOR_CLOSING) {
			double da = simdt * GEAR_OPERATING_SPEED;
			//PlayVesselWave3(MyID,hydraulics,LOOP,225,0);
			if (gear_status == DOOR_CLOSING) {
				if (gear_proc > 0.0){
					gear_proc = max (0.0, gear_proc-da);
					battery_draw_001 += ((.5/oapiGetFrameRate()) * oapiGetTimeAcceleration ());
				}
				else {
					gear_status = DOOR_CLOSED;
					 
					oapiTriggerRedrawArea (2, 0, AID_GEARINDICATOR);
					//StopVesselWave3(MyID,hydraulics);
					PlayVesselWave3(MyID,gear_retracted,NOLOOP,225,0);
				    
				}
			} else  { // door opening
				if (gear_proc < 1.0){
					gear_proc = min (1.0, gear_proc+da) ;
				battery_draw_001 += ((.5/oapiGetFrameRate()) * oapiGetTimeAcceleration ());
				}
				    
				else {
					
					gear_status = DOOR_OPEN;
					//oapiTriggerRedrawArea (2, 0, AID_GEARINDICATOR);
					//StopVesselWave3(MyID,hydraulics);
					PlayVesselWave3(MyID,GEARDOWN,NOLOOP,225,0);
				    
				}
			}
			SetAnimation (anim_gear, gear_proc);
			SetGearParameters (gear_proc);
			//UpdateStatusIndicators();
		}
	

// animate revereser
		if (REVERSER_STSTUS >= DOOR_CLOSING) {
			double da = simdt * REVERSER_OPERATING_SPEED;
			
			if (REVERSER_STSTUS == DOOR_CLOSING) {
				if (reverser_proc > 0.0){
					reverser_proc = max (0.0, reverser_proc-da);
					battery_draw_001 += ((.5/oapiGetFrameRate()) * oapiGetTimeAcceleration ());
				}
				else {
					REVERSER_STSTUS = DOOR_CLOSED;
				TURBINE_REVERSER = SYSTEM_OFF;	 
					//revertEngine(2);
					
					
				    
				}
			} else  { // door opening
				if (reverser_proc < 1.0){
					reverser_proc = min (1.0, reverser_proc+da) ;
				battery_draw_001 += ((.5/oapiGetFrameRate()) * oapiGetTimeAcceleration ());
				}
				    
				else {
					
					REVERSER_STSTUS = DOOR_OPEN;
					TURBINE_REVERSER = SYSTEM_ON;
					//revertEngine(2);
				    
				}
			}
			SetAnimation (anim_reverser, reverser_proc);
			
		}
	//end of can_power if


// animate bay doors
		if(MASTER_BAY_DOORS_POWER)
		{
			if(BAY_DOOR_FORWARD_RIGHT_POWER)
			{

		if (BAY_DOOR_FORWARD_RIGHT_STATUS >= DOOR_CLOSING) {
			double da = simdt * BAY_DOOR_OPERATING_SPEED;
			
			if (BAY_DOOR_FORWARD_RIGHT_STATUS == DOOR_CLOSING) {
				if (bay_door_forward_right_proc > 0.0){
					bay_door_forward_right_proc = max (0.0, bay_door_forward_right_proc-da);
					battery_draw_001 += ((.5/oapiGetFrameRate()) * oapiGetTimeAcceleration ());
				}
				else {
					BAY_DOOR_FORWARD_RIGHT_STATUS = DOOR_CLOSED;
				 
					
					
					
				    
				}
			} else  { // door opening
				if (bay_door_forward_right_proc < 1.0){
					bay_door_forward_right_proc = min (1.0, bay_door_forward_right_proc+da) ;
				battery_draw_001 += ((.5/oapiGetFrameRate()) * oapiGetTimeAcceleration ());
				}
				    
				else {
					
					BAY_DOOR_FORWARD_RIGHT_STATUS = DOOR_OPEN;
					
					
				    
				}
			}
			SetAnimation (anim_bay_door_forward_right, bay_door_forward_right_proc);
			
		}
			}// end of BAY_DOOR_FORWARD_RIGHT_POWER

		

        if(BAY_DOOR_FORWARD_LEFT_POWER)
		{
		if (BAY_DOOR_FORWARD_LEFT_STATUS >= DOOR_CLOSING) {
			double da = simdt * BAY_DOOR_OPERATING_SPEED;
			
			if (BAY_DOOR_FORWARD_LEFT_STATUS == DOOR_CLOSING) {
				if (bay_door_forward_left_proc > 0.0){
					bay_door_forward_left_proc = max (0.0, bay_door_forward_left_proc-da);
					battery_draw_001 += ((.5/oapiGetFrameRate()) * oapiGetTimeAcceleration ());
				}
				else {
					BAY_DOOR_FORWARD_LEFT_STATUS = DOOR_CLOSED;
				 
					
					
					
				    
				}
			} else  { // door opening
				if (bay_door_forward_left_proc < 1.0){
					bay_door_forward_left_proc = min (1.0, bay_door_forward_left_proc+da) ;
				battery_draw_001 += ((.5/oapiGetFrameRate()) * oapiGetTimeAcceleration ());
				}
				    
				else {
					
					BAY_DOOR_FORWARD_LEFT_STATUS = DOOR_OPEN;
					
					
				    
				}
			}
			SetAnimation (anim_bay_door_forward_left, bay_door_forward_left_proc);
			
		}

		} //end of BAY_DOOR_FORWARD_LEFT_POWER

	if(BAY_DOOR_AFT_LEFT_POWER)
	{	
		if (BAY_DOOR_AFT_LEFT_STATUS >= DOOR_CLOSING) {
			double da = simdt * BAY_DOOR_OPERATING_SPEED;
			
			if (BAY_DOOR_AFT_LEFT_STATUS == DOOR_CLOSING) {
				if (bay_door_aft_left_proc > 0.0){
					bay_door_aft_left_proc = max (0.0, bay_door_aft_left_proc-da);
					battery_draw_001 += ((.5/oapiGetFrameRate()) * oapiGetTimeAcceleration ());
				}
				else {
					BAY_DOOR_AFT_LEFT_STATUS = DOOR_CLOSED;
				 
					
					
					
				    
				}
			} else  { // door opening
				if (bay_door_aft_left_proc < 1.0){
					bay_door_aft_left_proc = min (1.0, bay_door_aft_left_proc+da) ;
				battery_draw_001 += ((.5/oapiGetFrameRate()) * oapiGetTimeAcceleration ());
				}
				    
				else {
					
					BAY_DOOR_AFT_LEFT_STATUS = DOOR_OPEN;
					
					
				    
				}
			}
			SetAnimation (anim_bay_door_aft_left, bay_door_aft_left_proc);
			
		}
	}

	if(BAY_DOOR_AFT_RIGHT_POWER)
	{
		if (BAY_DOOR_AFT_RIGHT_STATUS >= DOOR_CLOSING) {
			double da = simdt * BAY_DOOR_OPERATING_SPEED;
			
			if (BAY_DOOR_AFT_RIGHT_STATUS == DOOR_CLOSING) {
				if (bay_door_aft_right_proc > 0.0){
					bay_door_aft_right_proc = max (0.0, bay_door_aft_right_proc-da);
					battery_draw_001 += ((.5/oapiGetFrameRate()) * oapiGetTimeAcceleration ());
				}
				else {
					BAY_DOOR_AFT_RIGHT_STATUS = DOOR_CLOSED;
				 
					
					
					
				    
				}
			} else  { // door opening
				if (bay_door_aft_right_proc < 1.0){
					bay_door_aft_right_proc = min (1.0, bay_door_aft_right_proc+da) ;
				battery_draw_001 += ((.5/oapiGetFrameRate()) * oapiGetTimeAcceleration ());
				}
				    
				else {
					
					BAY_DOOR_AFT_RIGHT_STATUS = DOOR_OPEN;
					
					
				    
				}
			}
			SetAnimation (anim_bay_door_aft_right, bay_door_aft_right_proc);
			
		}
	}

}// end if power for bay doors


	
	
	// animate nose cone

		if (NOSE_CONE_STATUS >= DOOR_CLOSING) {
			double da = simdt * NOSE_OPERATING_SPEED;
			if (NOSE_CONE_STATUS == DOOR_CLOSING) {
				if (nose_cone_proc > 0.0){
					nose_cone_proc = max (0.0, nose_cone_proc-da);
				battery_draw_001 += ((.5/oapiGetFrameRate()) * oapiGetTimeAcceleration ());}
				else {
					NOSE_CONE_STATUS = DOOR_CLOSED;
					Crew.SetAirlockDoorState(false); 
					oapiTriggerRedrawArea (2, 0, AID_NOSECONEINDICATOR);
				}
			} else { // door opening
				if (nose_cone_proc < 1.0){
					nose_cone_proc = min (1.0, nose_cone_proc+da);
				battery_draw_001 += ((.5/oapiGetFrameRate()) * oapiGetTimeAcceleration ());}
				else {
					NOSE_CONE_STATUS = DOOR_OPEN;
					Crew.SetAirlockDoorState(true);
				}
			}
			SetAnimation (anim_nose, nose_cone_proc);
			//UpdateStatusIndicators();
		}
	


// animate air lock tube

		if (AIRLOCK_TUBE_STATUS >= DOOR_CLOSING) {
			double da = simdt * AIR_LOCK_TUBE_OPERATING_SPEED;
			if (AIRLOCK_TUBE_STATUS == DOOR_CLOSING) {
				if (air_lock_tube_proc > 0.0){
					air_lock_tube_proc = max (0.0, air_lock_tube_proc-da);
				battery_draw_001 += ((.5/oapiGetFrameRate()) * oapiGetTimeAcceleration ());}
				else {
					AIRLOCK_TUBE_STATUS = DOOR_CLOSED;
					
				}
			} else { // door opening
				if (air_lock_tube_proc < 1.0){
					air_lock_tube_proc = min (1.0, air_lock_tube_proc+da);
				battery_draw_001 += ((.5/oapiGetFrameRate()) * oapiGetTimeAcceleration ());}
				else {
					AIRLOCK_TUBE_STATUS = DOOR_OPEN;
					
				}
			}
			SetAnimation (anim_air_lock_tube, air_lock_tube_proc);
			
		}














	// animate escape ladder

		if (ladder_status >= DOOR_CLOSING) {
			double da = simdt * LADDER_OPERATING_SPEED;
			if (ladder_status == DOOR_CLOSING) {
				if (ladder_proc > 0.0)
					ladder_proc = max (0.0, ladder_proc-da);
				else {
					ladder_status = DOOR_CLOSED;
					//oapiTriggerPanelRedrawArea (2, AID_NOSECONEINDICATOR);
				}
			} else {
				if (ladder_proc < 1.0)
					ladder_proc = min (1.0, ladder_proc+da);
				else {
					ladder_status = DOOR_OPEN;
					//oapiTriggerPanelRedrawArea (2, AID_NOSECONEINDICATOR);
				}
			}
			SetAnimation (anim_ladder, ladder_proc);
		}


	
		// animate top hatch
		if (hatch_status >= DOOR_CLOSING) {
			double da = simdt * HATCH_OPERATING_SPEED;
			if (hatch_status == DOOR_CLOSING) {
				if (hatch_proc > 0.0)
					hatch_proc = max (0.0, hatch_proc-da);
				else {
					hatch_status = DOOR_CLOSED;
					//oapiTriggerPanelRedrawArea (2, AID_NOSECONEINDICATOR);
				}
			} else {
				if (hatch_proc < 1.0)
					hatch_proc = min (1.0, hatch_proc+da);
				else {
					hatch_status = DOOR_OPEN;
					//oapiTriggerPanelRedrawArea (2, AID_NOSECONEINDICATOR);
				}
			}
			SetAnimation (anim_hatch, hatch_proc);
			//UpdateStatusIndicators();
		}
	
	
		// animate outer airlock
		if (OUTER_AIRLOCK_DOOR_STATUS >= DOOR_CLOSING) {
			double da = simdt * AIRLOCK_OPERATING_SPEED;
			if (OUTER_AIRLOCK_DOOR_STATUS == DOOR_CLOSING) {
				if (outer_air_lock_proc > 0.0)
					outer_air_lock_proc = max (0.0, outer_air_lock_proc-da);
				else {
					OUTER_AIRLOCK_DOOR_STATUS = DOOR_CLOSED;
					//oapiTriggerPanelRedrawArea (1, AID_AIRLOCKINDICATOR);
				}
			} else { // door opening
				if (outer_air_lock_proc < 1.0)
					outer_air_lock_proc = min (1.0, outer_air_lock_proc+da);
				else {
					OUTER_AIRLOCK_DOOR_STATUS = DOOR_OPEN;
					//oapiTriggerPanelRedrawArea (1, AID_AIRLOCKINDICATOR);
				}
			}
			SetAnimation (anim_olock, outer_air_lock_proc);
			//UpdateStatusIndicators();
		}
	

	
		// animate inner airlock
		if (INNER_AIRLOCK_DOOR_STATUS >= DOOR_CLOSING) {
			double da = simdt * AIRLOCK_OPERATING_SPEED;
			if (INNER_AIRLOCK_DOOR_STATUS == DOOR_CLOSING) {
				if (inner_air_lock_proc > 0.0)
					inner_air_lock_proc = max (0.0, inner_air_lock_proc-da);
				else {
					INNER_AIRLOCK_DOOR_STATUS = DOOR_CLOSED;
					//oapiTriggerPanelRedrawArea (1, AID_AIRLOCKINDICATOR);
				}
			} else { // door opening
				if (inner_air_lock_proc < 1.0)
					inner_air_lock_proc = min (1.0, inner_air_lock_proc+da);
				else {
					INNER_AIRLOCK_DOOR_STATUS = DOOR_OPEN;
					//oapiTriggerPanelRedrawArea (1, AID_AIRLOCKINDICATOR);
				}
			}
			SetAnimation (anim_ilock, inner_air_lock_proc);
			//UpdateStatusIndicators();
		}
	

	
		// animate radiator
		if (radiator_status >= DOOR_CLOSING) {
			double da = simdt * RADIATOR_OPERATING_SPEED;
			if (radiator_status == DOOR_CLOSING) { // retract radiator
				if (radiator_proc > 0.0) radiator_proc = max (0.0, radiator_proc-da);
				else                     radiator_status = DOOR_CLOSED;
			} else {                               // deploy radiator
				if (radiator_proc < 1.0) radiator_proc = min (1.0, radiator_proc+da);
				else                     radiator_status = DOOR_OPEN;
			}
			SetAnimation (anim_radiator, radiator_proc);
			//UpdateStatusIndicators();
		}
	

	
		// animate airbrake
		if (brake_status >= DOOR_CLOSING) {
			double da = simdt * AIRBRAKE_OPERATING_SPEED;
			if (brake_status == DOOR_CLOSING) { // retract brake
				if (brake_proc > 0.0) brake_proc = max (0.0, brake_proc-da);
				else                  brake_status = DOOR_CLOSED;
			} else {                            // deploy brake
				if (brake_proc < 1.0) brake_proc = min (1.0, brake_proc+da);
				else                  brake_status = DOOR_OPEN;
			}
			SetAnimation (anim_brake, brake_proc);
			//UpdateStatusIndicators();
		}
	

	if (hatch_vent && simt > hatch_vent_t + 1.0) {
		DelExhaustStream (hatch_vent);
		hatch_vent = NULL;
	}
	//after animations so we can remove the nessasary amount of power
	sprintf(oapiDebugString(),"Battery charge is %.3f battery_charge_Rate is %.4f, can_charge[1] %d battery_draw %3f",battery_charge_001,battery_charge_Rate_001,can_charge[1],battery_draw_001); // Just to test
	Battery_handle();
	


	}
